package com.hqs.listener.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hqs.listener.R;

import butterknife.ButterKnife;

/**
 * Created by super on 2017/11/11.
 */

public class TabBarView extends LinearLayout{


    private LinearLayout contentView;

    private ImageView imgView1;
    private ImageView imgView2;
    private ImageView imgView3;
    private RelativeLayout rl1;
    private RelativeLayout rl2;
    private RelativeLayout rl3;
    private ImageView imgPlayBackground;
    private OnClickTabBarListener onClickTabBarListener;



    public TabBarView(Context context) {
        super(context);
        setupContent();
    }

    public TabBarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setupContent();
    }

    private void setupContent(){
        contentView = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.view_tabbar, null);
        addView(contentView);
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        contentView.setLayoutParams(params);

        ButterKnife.bind(this, contentView);

        imgView1 = contentView.findViewById(R.id.imgView1);
        imgView2 = contentView.findViewById(R.id.imgView2);
        imgView3 = contentView.findViewById(R.id.imgView3);
        imgPlayBackground = contentView.findViewById(R.id.imgPlayBg);

        rl1 = contentView.findViewById(R.id.rl1);
        rl2 = contentView.findViewById(R.id.rl2);
        rl3 = contentView.findViewById(R.id.rl3);

        int selectedColor = getResources().getColor(R.color.colorPrimary);
        ColorStateList colorStateList = colorStateList(Color.LTGRAY, selectedColor);
        imgView1.setImageTintList(colorStateList);

        rl1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                select(0);
            }
        });

        imgView3.setImageTintList(colorStateList);
        rl3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                select(2);
            }
        });

        rl2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                select(1);
            }
        });
    }

    public void select(int index){

        imgView1.setEnabled(true);
        imgView3.setEnabled(true);
        imgPlayBackground.setImageResource(R.mipmap.play_bg);

        switch (index){
            case 0:
                imgView1.setEnabled(false);
                break;
            case 2:
                imgView3.setEnabled(false);
                break;
            default:
                imgPlayBackground.setImageResource(R.mipmap.play_bg_s);
                break;
        }

        if (onClickTabBarListener != null){
            onClickTabBarListener.onClick(index);
        }
    }

    private ColorStateList colorStateList(int normal, int unable){
        int pressed = normal;
        int focused = normal;
        int[] colors = new int[] { pressed, focused, normal, focused, unable, normal };
        int[][] states = new int[6][];
        states[0] = new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled };
        states[1] = new int[] { android.R.attr.state_enabled, android.R.attr.state_focused };
        states[2] = new int[] { android.R.attr.state_enabled };
        states[3] = new int[] { android.R.attr.state_focused };
        states[4] = new int[] { android.R.attr.state_window_focused };
        states[5] = new int[] {};
        ColorStateList colorList = new ColorStateList(states, colors);
        return colorList;
    }

    public void setOnClickTabBarListener(OnClickTabBarListener onClickTabBarListener) {
        this.onClickTabBarListener = onClickTabBarListener;
    }



    public interface OnClickTabBarListener {
        void onClick(int position);
    }
}



